﻿using House.Common;
using House.DTO;
using House.IService;
using House.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace House.Admin.Controllers
{
    
    public class AdminUserController : Controller
    {
        public IAdminUserService adminUserService { get; private set; }
        public IRoleService  roleService { get;  set; }

        public ICityService cityService { get; set; }
        public AdminUserController(IAdminUserService admin,IRoleService role,ICityService city)
        {
            adminUserService = admin;
            roleService = role;
            cityService = city;
        }
        // GET: AdminUser
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetList(int start, int length,string name="")
        {
            long rowCount = 0;
            var list = adminUserService.GetList(start, length, name, ref rowCount);
            var result = new { data = list, recordsTotal = rowCount, recordsFiltered = rowCount };
            return Json(result);
        }
        [PermissionAuthorize("addPeople")]
        public ActionResult Create()
        {
            var roles = roleService.GetList();
            var cities = cityService.GetList();
            ViewBag.cityList = cities.Select( p => new SelectListItem()
            {
                Value=p.Id.ToString(),
                Text=p.Name
            }).ToList();
            return View(roles);
        }

        [HttpPost]
        [PermissionAuthorize("addPeople")]
        public ActionResult Create(AdminUserAddDTO user)
        {
            if (ModelState.IsValid)
            {

                return Json(adminUserService.Insert(user));
            }
            return Json(AjaxResult<bool>.Fail("添加失败"));
        }

        public ActionResult Edit(long id)
        {
            var admin = adminUserService.GetEdit(id);
            var cities = cityService.GetList();
            ViewBag.cityList = new SelectList(cities,"Id","Name",admin.CityId);
            //ViewBag.cityList = cities.Select(p => new SelectListItem()
            //{
            //    Value = p.Id.ToString(),
            //    Text = p.Name
            //}).ToList();
            var roles = roleService.GetList();
            ViewBag.releList = roles;

            return View(admin);
        }
        [HttpPost]
        public ActionResult Edit(AdminUserEditDTO admin)
        {
            if (ModelState.IsValid)
            {

                return Json(adminUserService.Update(admin));
            }
            return Json(AjaxResult<bool>.Fail("修改失败"));
        }
            public ActionResult Delete(long id)
        {
            var result = adminUserService.Delete(id);
            return Json(result);
        }

    }
}